package com.inspectortime.webapp.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 * Will simply render a view based on the path name (i.e. /bla.html will render bla.ftl)
 * 
 * @author mkvalsvik
 * 
 */
public class PathAsViewController implements Controller {

	private Log log = LogFactory
			.getLog(PathAsViewController.class);

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		// Strip the leading slash and trailing ".xxx" to get view name
		String viewName = RequestPathUtils.extractViewNameFromPath(request);
		log.debug("Resolved path " + request.getServletPath() + " to view "
				+ viewName);
		ModelAndView mv = new ModelAndView(viewName);
		return mv;
		
	}

}
